package com.it.LoginSystem.service.impl;

import com.it.LoginSystem.Pojo.Emp;
import com.it.LoginSystem.mapper.EmpMapper;

import com.it.LoginSystem.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    /**
     * 用户登录
     * @param emp
     * @return
     */
    @Override
    public Emp login(Emp emp){

/*未加密
        return empMapper.getByusernameAndPassword(emp);
*/
        String password = emp.getPassword();

        //密码比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        Emp emp1 = emp;
        emp1.setPassword(password);
        return emp1;

    }

    /**
     * 用户注册
     * @param emp
     * @return
     */
    @Override
    public Emp register(Emp emp) {

        Emp u = empMapper.selectByUsername(emp.getUsername());
            if(u == null) {
                empMapper.insert(emp);
                return emp;
            }
            else {
                return null;
            }
    }


    /**
     * 用户注册（加密）
     * @param emp
     * @return
     */
    @Override
    public Emp registerSSL(Emp emp) {
        String password = emp.getPassword();

        Emp u = empMapper.selectByUsername(emp.getUsername());
        if(u == null) {
            password = DigestUtils.md5DigestAsHex(emp.getPassword().getBytes());
            Emp emp1 = emp;
            emp.setPassword(password);
            empMapper.insert(emp1);
            return emp;
        }
        else {
            return null;
        }
    }

}
